package com.fulihui.usercore.biz.shard;

import com.fulihui.systemcore.condition.WechatConfigCondition;
import com.fulihui.systemcore.dto.WechatConfig;
import com.fulihui.systemcore.enums.EnvTypeEnum;
import com.fulihui.usercore.common.integration.WechatConfigServiceClient;
import org.near.toolkit.common.EnumUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 微信配置工厂
 * @author Created by Willard.Hu on 2016/6/2.
 */
@Component
public class WechatConfigFactory {
    private final transient Logger    LOG            = LoggerFactory
                                                         .getLogger(WechatConfigFactory.class);
    @Autowired
    private WechatConfigServiceClient wechatConfigServiceClient;
    @Value("${product.env}")
    private String                    productEnv;

    // FIXME [huwei] 移到配置文件去
    private String                    FULIHUI_WECHAT = "FULIHUI_WECHAT";

    private WechatConfig              wechatConfig;

    @PostConstruct
    public void init() {
        try {
            WechatConfigCondition condition = new WechatConfigCondition();
            condition.setEnvType(EnumUtil.queryByCode(productEnv, EnvTypeEnum.class));
            condition.setConfigCode(FULIHUI_WECHAT);
            WechatConfig wechatConfig = wechatConfigServiceClient.querySingle(condition);
            this.wechatConfig = wechatConfig;
            LOG.debug("获取的wechatConfig:{}", wechatConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public WechatConfig get() {
        return wechatConfig;
    }

}
